<template>
	<view class="container">
		<view class="container-c">
			<view class="caritem" v-for="item in carList" :key="item.id">
				<view class="top">
					<evan-checkbox v-model="item.isCheck" shape="square" primary-color="#354e44"></evan-checkbox>
					<image :src="item.productPic" mode=""></image>
					<view class="text">
						<view class="name">{{item.productName}}</view>
						<view class="other">编号:{{item.productSkuId}}</view>
						<view class="other">价格:¥{{item.price}}</view>
						<view class="other">数量:{{item.quantity}}</view>
					</view>
				</view>
				<view class="number">
					<view @tap="changeNum(item)" class="cut">
						-
					</view>
					<view class="num">
						{{item.quantity}}
					</view>
					<view @tap="changeNum(item,1)" class="add">
						+
					</view>
				</view>
			</view>
		</view>
		<view class="bottom">
			<evan-checkbox v-model="allCheck" shape="square" primary-color="#354e44">全选</evan-checkbox>
			<view class="right">
				<view class="price">
					合计:¥{{total}}

				</view>
				<view class="btn" @tap="goOrder">
					结算
				</view>
			</view>

		</view>

	</view>
</template>
<script>
	import {
		addPreOrder
	} from "@/api/list/order/index.js"
	import {
		findAllCartItem
	} from "@/api/user/cart/index.js"
	export default {
		data() {
			return {
				carList: [],
				checkItem: []
			};
		},
		onLoad() {
			findAllCartItem().then(res => {
				console.log(res)
				res.data.items.forEach(i => i.isCheck = false)
				this.carList = res.data.items
			})
		},
		computed: {
			allCheck: {
				get() {
					return this.carList.every(ele => ele.isCheck)
				},
				set(e) {
					this.carList.forEach(i => i.isCheck = e)
				}
			},
			total() {
				return this.carList.reduce((total, current) => {
					if (current.isCheck) {
						return total = total + current.price * current.quantity;
					} else {
						return total
					}
				}, 0)
			}

		},
		methods: {
			changeNum(bool, type) {
				if (type == 1) {
					bool.quantity++
				} else {
					if (bool.quantity > 1) {
						bool.quantity--
					}
				}
			},
			goOrder() {
				var arr = []
				this.carList.forEach(ele => {
					var obj = {
						"cartId": ele.id,
						"productId": ele.productId,
						"productQuantity": ele.quantity,
						"productSkuId": ele.productSkuId
					}
					arr.push(obj)
				})
				var params = {
					"orderItemList":arr
				}
				addPreOrder(params).then(res => {
					console.log(res)
					var id=res.data.orderId;
					if(res.success==true){
						uni.navigateTo({
							url:"/pages/list/order/order?id="+id
						})
					}else{
						uni.showToast({
							title:"您没有购买任何商品"
						})
					}
				})
			}
		}
	}
</script>
<style lang="scss" scoped>
	.container {
		width: 100%;
		background-color: #f1ece7;
		height: 100vh;
	}

	.container-c {
		width: 670rpx;
		margin: 0 auto;
		padding-top: 30rpx;
	}

	.caritem {
		display: flex;
		flex-direction: column;
		align-items: center;
		background-color: #fff;
		padding: 20rpx 0;
		border-radius: 24rpx;
		margin-top: 30rpx;

		.top {
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.number {
			margin-left: 350rpx;
		}

		image {
			width: 120rpx;
			height: 136rpx;
			margin: 0 20rpx;
		}

		.text {
			.name {
				font-size: 26rpx;
				color: #3e3e3e;
				line-height: 36rpx;
				width: 160rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			.other {
				font-size: 24rpx;
				color: #8d8d8d;
				line-height: 36rpx;
			}
		}
	}

	.number {
		display: flex;
		align-items: center;

		.cut,
		.add {
			width: 60rpx;
			height: 60rpx;
			background-color: #f1ece7;
			border-radius: 50%;
			text-align: center;
			line-height: 60rpx;
		}

		.num {
			width: 90rpx;
			height: 60rpx;
			background-color: #f1ece7;
			border-radius: 40%;
			text-align: center;
			line-height: 60rpx;
			margin-left: 10rpx;
		}

		.add {
			margin-left: 10rpx;
		}
	}

	.bottom {
		position: fixed;
		left: 0;
		bottom: 0;
		display: flex;
		justify-content: space-around;
		width: 100%;
		height: 150rpx;
		background-color: #fff;
		display: flex;
		// color: #FFFFFF;
		align-items: center;

		.right {
			display: flex;
			align-items: center;

			.price {
				font-size: 32rpx;
			}

			.btn {
				width: 208rpx;
				height: 80rpx;
				background: #354e44;
				border-radius: 28rpx;
				color: #fff;
				font-size: 36rpx;
				line-height: 80rpx;
				text-align: center;
				margin-left: 10rpx;
			}
		}
	}
</style>
